package Servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/getBirth")
public class getBirth extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //从数据库中获取所有人的生日
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        List<birthBeen> dates = new ArrayList<>();

        try {
            conn = JDBCutil.getConnection();
            ps = conn.prepareStatement("select * from t_birthday");
            rs = ps.executeQuery();

            while (rs.next()){
                String name = rs.getString("name");
                int age = rs.getInt("age");
                String birthday = rs.getString("birthday");
                //封装成对象并加入集合
                dates.add(new birthBeen(name,age,birthday));
            }
        } catch (SQLException | ClassNotFoundException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCutil.closeRs(rs,ps, conn);
        }

        //将集合放入请求域
        request.setAttribute("dates",dates);
        //转发
        request.getRequestDispatcher("/show.jsp").forward(request,response);//这里写不需要加项目名的路径
    }
}
